package com.ocom.accesservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.accesservice.dao.IDaoPersonnelSum;
import com.ocom.accesservice.service.PersonnelSumService;
import com.ocom.accesservice.vo.PersonnelSumVo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PersonnelSumServiceImpl extends ServiceImpl<IDaoPersonnelSum, PersonnelSumVo> implements PersonnelSumService {

    @Autowired
    private IDaoPersonnelSum iDaoPersonnelSum;

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "companyId")
    @Override
    public List<PersonnelSumVo> selectPersonnelSum(Long companyId) {
        QueryWrapper<PersonnelSumVo> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("region_id","com_id",
                "SUM(case when region_online =1 then 1 else 0 end) onlineSum",
                "SUM(case when region_orig_id=region_id and region_online =1 then 1 else 0 end) InSum",
                "SUM(case when region_orig_id !=region_id and region_online =1  then 1 else 0 end) otherSum",
                "SUM(case when (region_orig_id=region_id and region_online =0) or (region_orig_id !=region_id) then 1 else 0 end) outSum",
                "SUM(case when region_online =1 and p_type=103 then 1 else 0 end) InSum103",
                "SUM(case when region_orig_id=region_id and region_online =1 and p_type=104 then 1 else 0 end) InSum104");
        queryWrapper.eq("com_id",companyId);
        queryWrapper.eq("status ",0);
        queryWrapper.groupBy("region_id");
        return iDaoPersonnelSum.selectList(queryWrapper);
    }
}
